package com.rhtchr.angelbaby.domain;

import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.apache.commons.lang.StringUtils;

/**
 * 文章信息。一个文章包含一个标题、摘要、作者、创建日期、多个段落。
 * 
 * @author limingju
 * 
 */
@Entity
@Table(name = "t_article")
public class Article {

	private int id;

	// 作者
	private String author;
	// 文章创建时间
	private Date insertTime;
	private Date updateTime;
	private Set<Paragraph> aragraphs = new HashSet<Paragraph>();

	// 标题
	private String title;

	// 摘要
	private String summary;
	// 子标题或者父标题
	private String subtitle;

	public Article() {
		super();
		Date tmp = Calendar.getInstance().getTime();
		insertTime = tmp;
		updateTime = tmp;
	}

	@OneToMany(targetEntity = Paragraph.class, mappedBy = "articleId")
	public Set<Paragraph> getAragraphs() {
		return aragraphs;
	}

	/**
	 * 返回文章的作者。如果没有录入文章的作者，那么默认为“佚名”。
	 * 
	 * @return
	 */
	@Column(name = "author_name", length = 64)
	public String getAuthor() {
		if (StringUtils.isEmpty(author)) {
			author = "佚名";
		}
		return author;
	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "id")
	public int getId() {
		return id;
	}

	@Column(name = "insert_time", nullable = false, updatable = false)
	public Date getInsertTime() {
		return insertTime;
	}

	@Column(name = "summary", length = 4096)
	public String getSummary() {
		return summary;
	}

	@Column(name = "update_time", nullable = false)
	public Date getUpdateTime() {
		return updateTime;
	}

	public void setAragraphs(Set<Paragraph> aragraphs) {
		this.aragraphs = aragraphs;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void setInsertTime(Date insertTime) {
		this.insertTime = insertTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	@Column(name = "title", length = 1024, nullable = false)
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setSummary(String summary) {
		this.summary = summary;
	}

	@Column(name = "sub_title", length = 1024)
	public String getSubtitle() {
		return subtitle;
	}

	public void setSubtitle(String subtitle) {
		this.subtitle = subtitle;
	}

}
